<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Contract extends CI_Controller {
 function __construct()
 {
   parent::__construct();
   $this->load->model('muser');
   $this->load->library('form_validation');
   $this->load->library('pagination');
 }
 function index(){ 	
 	 	
 	 $config['base_url'] = base_url().'contract/index'; 
	 $config['total_rows'] = $this->muser->num_rows('Contract');
	 
	 $config['per_page'] = 10; // xác định số record ở mỗi trang  
     $config['uri_segment'] = 3; // xác định segment chứa page number  
	 $this->pagination->initialize($config); 
	 $data['result']=$this->muser->getAllContract($config['per_page'],$this->uri->segment(3));	
	  if ($error=$this->session->userdata('error')) {
	 	$data['error']=$error;
		$this->session->unset_userdata('error'); 
	 }
	 $this->load->view('_header');
     $this->load->view('contract/contract_view',$data); 
     $this->load->view('_footer');
 }
function insert(){
	if ($this->input->post('Contract_ID')!=NULL)
 	 {
 	 	$this->form_validation->set_rules('Contract_ID','Contract_ID','trim|integer|callback_check_Contract');		
		if ($this->form_validation->run()==FALSE){
			$data['error']='Contract_ID is existed';			
		}else
		{
			$DT=array(
						"Contract_ID"=>$this->input->post('Contract_ID'),
						"Staff_ID"=>$this->input->post('Staff_ID'),
						"JoiningDate"=>$this->input->post('JoiningDate'),
						"Location"=>$this->input->post('Location'),
						"Department_ID"=>$this->input->post('Department_ID'),
						"Session_ID"=>$this->input->post('Session_ID'),
						"WorkLevel"=>$this->input->post('WorkLevel'),
						"Position"=>$this->input->post('Position'),
						"ContractType_ID"=>$this->input->post('ContractType_ID'),
						"EffectiveDate"=>$this->input->post('EffectiveDate'),
						"ExpiredDate"=>$this->input->post('ExpiredDate'),
						"Prodemotion"=>$this->input->post('Prodemotion'),
						"SalaryScale"=>$this->input->post('SalaryScale'),
						"ExitDate"=>$this->input->post('ExitDate'),
						"ResignationDate"=>$this->input->post('ResignationDate'),
						"DayNight"=>$this->input->post('DayNight')						
				);
			if ($this->muser->checkContract($DT['Staff_ID'],$DT['EffectiveDate'])){
				$this->muser->addData('Contract',$DT);
				$data['error']='Insert successful!';	
			}else{
				$data['error']='Have another Contract duplicate time ';				
			}			
		} 	
		$this->session->set_userdata($data);
		redirect('contract/index');			
 	 }
	
	$data['staff']	= $this->getStaffID();
	$data['department']=$this->getDepartment();
	$data['section']=$this->getSection();
	$data['contractType']=$this->getContractType();
	$data['workLevel']=$this->getWorkLevelID();
	$this->load->view('_header');
     $this->load->view('contract/insertContract_view',$data); 
     $this->load->view('_footer');
}

function view($str){
	$result=$this->muser->getRdata('Contract','Contract_ID',$str);
	$data['result']=$result;
	$this->load->view('_header');
	$this->load->view('contract/viewContract_view',$data);	
	$this->load->view('_footer');
}
 function delete($Contract_ID)
 {
 	if ($this->muser->checkData('Annex','Contract_ID',$Contract_ID)){
			$this->muser->delColumn('Contract','Contract_ID',$Contract_ID);
			redirect('contract/index'); 		
 	}else{
		$error['error']= $Contract_ID.' is a foreign key!';
		$this->session->set_userdata($error);
		redirect('contract/index'); 		
 	}

}
 
 function edit1 ()
 {
 	$DT=array(
						"Contract_ID"=>$this->input->post('Contract_ID'),
						"Staff_ID"=>$this->input->post('Staff_ID'),
						"JoiningDate"=>$this->input->post('JoiningDate'),
						"Location"=>$this->input->post('Location'),
						"Department_ID"=>$this->input->post('Department_ID'),
						"Session_ID"=>$this->input->post('Session_ID'),
						"WorkLevel"=>$this->input->post('WorkLevel'),
						"Position"=>$this->input->post('Position'),
						"ContractType_ID"=>$this->input->post('ContractType_ID'),
						"EffectiveDate"=>$this->input->post('EffectiveDate'),
						"ExpiredDate"=>$this->input->post('ExpiredDate'),
						"Prodemotion"=>$this->input->post('Prodemotion'),
						"SalaryScale"=>$this->input->post('SalaryScale'),
						"ExitDate"=>$this->input->post('ExitDate'),
						"ResignationDate"=>$this->input->post('ResignationDate'),
						"DayNight"=>$this->input->post('DayNight')							
				);
	$this->muser->updateData('Contract','Contract_ID',$DT['Contract_ID'],$DT);
	$data['error']='Edit successful!';	 	
	redirect('contract/index',$data);		
 } 
 function edit($str)
 {	
 	$result=$this->muser->getRdata('Contract','Contract_ID',$str);
	$data['result']=$result;
	$data['department']=$this->getDepartment();
	$data['section']=$this->getSection();
	$data['contractType']=$this->getContractType();
	$data['workLevel']=$this->getWorkLevelID();
 	$this->load->view('_header');
	$this->load->view('contract/editContract_view',$data);	
	$this->load->view('_footer');
 }
 
 function check_Contract($str){
 		return $this->muser->checkData('Contract','Contract_ID',$str);		
 }
 function getStaffID(){
 	 $lab1=$this->muser->getData('Staff');
	 foreach ($lab1 as $row) {
		 $lab[$row->Staff_ID]=$row->Staff_ID.'-'.$row->FullName;
	 }
	 return $lab;
 }
  function getDepartment(){
 	 $lab1=$this->muser->getData('Department');
	 foreach ($lab1 as $row) {
		 $lab[$row->Department_ID]=$row->Department_ID.'-'.$row->Department;
	 }
	 return $lab;
 }
   function getSection(){
 	 $lab1=$this->muser->getData('Session');
	 foreach ($lab1 as $row) {
		 $lab[$row->Session_ID]=$row->Session_ID.'-'.$row->SessionName;
	 }
	 return $lab;
 }
 function getContractType(){
 	 $lab1=$this->muser->getData('ContractType');
	 foreach ($lab1 as $row) {
		 $lab[$row->ContractType_ID]=$row->ContractType_ID.'-'.$row->ContractTypeName;
	 }
	 return $lab;
 }
 function getWorkLevelID(){
 	 $lab1=$this->muser->getData('WorkLevel');
	 foreach ($lab1 as $row) {
		 $lab[$row->WorkLevel_ID]=$row->WorkLevel_ID.'-'.$row->Position;
	 }
	 return $lab;
 }
}